package com.jsgt.greekfire.ui.home.menu.pool

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.PoolOrderBean
import com.jsgt.greekfire.common.util.PayPassWordUtil
import com.jsgt.greekfire.common.widget.maxDecimalPlace
import com.jsgt.greekfire.viewmodel.PrivilegeViewModel
import kotlinx.android.synthetic.main.activity_pool_join.*
import kotlinx.android.synthetic.main.view_title.*

class PoolJoinActivity : BaseActivity(), View.OnClickListener {
    private var poolType: String? = null
    private val privilegeViewModel: PrivilegeViewModel by lazy {
        ViewModelProvider(this).get(PrivilegeViewModel::class.java)
    }

    private val orderAdapter: PoolJoinOrderAdapter by lazy {
        PoolJoinOrderAdapter()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_pool_join
    }

    override fun main() {
        poolType = intent?.getStringExtra("type")
        if (TextUtils.isEmpty(poolType)) {
            finish()
        }
        if (poolType.equals("CLOUD_MINE")) {
            title_tv.text = getString(R.string.pool)
        } else {
            title_tv.text = getString(R.string.pool_p)
        }
//        edtAmount.maxDecimalPlace(4)
        txvJoin.setOnClickListener(this)
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = orderAdapter
        orderAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                PayPassWordUtil.show(this@PoolJoinActivity, "撤销", "") {
                    privilegeViewModel.poolOut(orderAdapter.getItem(position).id.toString(), it)
                }
            }
        })

        privilegeViewModel.poolOrderList(poolType!!)
        privilegeViewModel.poolOrderListData.observe(this, Observer {
            if (it.isNotEmpty()) {
                tvOrder.visibility = View.VISIBLE
                recycler.visibility = View.VISIBLE
                orderAdapter.setNewInstance(it as MutableList<PoolOrderBean>?)
            } else {
                tvOrder.visibility = View.GONE
                recycler.visibility = View.GONE
            }
        })
        privilegeViewModel.poolInData.observe(this, Observer {
            privilegeViewModel.poolOrderList(poolType!!)
            showToast("参与成功")
            edtAmount.setText("")
            edtPassword.setText("")
            txvJoin.isEnabled = true
        })
        privilegeViewModel.poolOutData.observe(this, Observer {
            privilegeViewModel.poolOrderList(poolType!!)
            showToast("撤销成功")
        })
        privilegeViewModel.errorLiveData.observe(this, Observer {
            showToast(it.toString())
            txvJoin.isEnabled = true
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txvJoin -> {
                if (allowJoin()) {
                    txvJoin.isEnabled = false
                    val password = edtPassword.text.toString()
                    privilegeViewModel.poolIn(
                        poolType!!,
                        password,
                        2,
                        edtAmount.text.toString().toDouble()
                    );
                }
            }
        }
    }

    private fun allowJoin(): Boolean {
        return when {
            TextUtils.isEmpty(edtAmount.text) -> {
                showToast(getString(R.string.pool_join_amount_input))
                false
            }
            TextUtils.isEmpty(edtPassword.text) -> {
                showToast(getString(R.string.pool_join_password_input))
                false
            }
            else -> {
                true
            }
        }
    }
}